var GameController = require("GameController");

cc.Class({
    extends: cc.Component,

    properties: {
        GM: {
            default: null,
            type: GameController
        },
        Option: null,
        Player: {
            default: null,
            type: cc.Node
        },
        UICamera: {
            default: null,
            type: cc.Node
        },
        MoveArrow:{
            default:[],
            type: [cc.Node]
        },
        Bacteria: null,
        Cameraa: null,
        NOtouching: true,
        GameSize: 1344,
        RingRadius: 830,
        ResolutionRatio: cc.v2(1080, 1920),
        BoundaryWidth: 50,
        YRLimit: null,// ResolutionRatio.y / (GameSize + RingRadius + BoundaryWidth)
        XRLimit: null, // ResolutionRatio.x / (GameSize + RingRadius + BoundaryWidth)
        Rlimit: null,   //取大值
        FPosX: null,    //视角应跟随的x坐标
        FPosY: null,    // 视角应跟随的y坐标
        FRatx: null,    // x轴上缩放倍数
        FRaty: null,     // y轴上缩放倍数
        TouchxinC: null, //点击到的点
        TouchyinC: null
    },

    setInput() {
        var self = this;
        self.node.on(cc.Node.EventType.TOUCH_START, function (event) { self.TouchStart(event, self) }, self);
        self.node.on(cc.Node.EventType.TOUCH_MOVE, function (event) { self.Touching(event, self) }, self);
        self.node.on(cc.Node.EventType.TOUCH_END, function (event) { self.TouchEnd(event, self) }, self);
    },

    TouchStart(event, self) {
        if(self.GM.GameStart && self.Bacteria.ShootAllowd){
        self.NOtouching = false;
        self.ArrowTurn(event, self);
        self.Bacteria.Arrow.active = true;
        self.TouchxinC = event.getLocation().x - self.ResolutionRatio.x / 2; //触摸屏幕坐标位置,中间为零点
        self.TouchyinC = event.getLocation().y - self.ResolutionRatio.y / 2;
        }
    },

    Touching(event, self) {
        if(self.GM.GameStart && self.Bacteria.ShootAllowd){
        self.ArrowTurn(event, self);
        self.TouchxinC = event.getLocation().x - self.ResolutionRatio.x / 2; //触摸屏幕坐标位置,中间为零点
        self.TouchyinC = event.getLocation().y - self.ResolutionRatio.y / 2;

        }
    },

    ArrowTurn: function (event, self) {
        var turn;
        if ((self.Cameraa.getCameraToWorldPoint(event.getLocation()).x - self.ResolutionRatio.x / 2 - self.Player.x) > 0) {
            turn = Math.PI - Math.atan2((self.Cameraa.getCameraToWorldPoint(event.getLocation()).y - self.ResolutionRatio.y / 2 - self.Player.y),
                (self.Cameraa.getCameraToWorldPoint(event.getLocation()).x - self.ResolutionRatio.x / 2 - self.Player.x));
        }
        else if ((self.Cameraa.getCameraToWorldPoint(event.getLocation()).x - self.ResolutionRatio.x / 2 - self.Player.x) < 0) {
            turn = 2 * Math.PI + Math.atan2((self.Cameraa.getCameraToWorldPoint(event.getLocation()).y - self.ResolutionRatio.y / 2 - self.Player.y),
                -(self.Cameraa.getCameraToWorldPoint(event.getLocation()).x - self.ResolutionRatio.x / 2 - self.Player.x));
        }
        else {
            if ((self.Cameraa.getCameraToWorldPoint(event.getLocation()).y - self.ResolutionRatio.y / 2 - self.Player.y) > 0) {
                turn = 0.5 * Math.PI;
            }
            else {
                turn = -0.5 * Math.PI;
            }
        }
        self.Bacteria.Arrow.rotation = turn / Math.PI * 180 - 90;
    },

    TouchEnd(event, self) {
        if(self.GM.GameStart && self.Bacteria.ShootAllowd){
        self.NOtouching = true;
        self.ArrowTurn(event, self);
        self.Bacteria.Arrow.active = false;  
        self.TouchxinC = event.getLocation().x - self.ResolutionRatio.x / 2; //触摸屏幕坐标位置,中间为零点
        self.TouchyinC = event.getLocation().y - self.ResolutionRatio.y / 2;
        self.ShootBacteria(event,self);
        }
    },
    // 射出细菌
    ShootBacteria(event,self){
        var TouchALong = Math.sqrt((self.Cameraa.getCameraToWorldPoint(event.getLocation()).x - self.ResolutionRatio.x / 2 - self.Player.x) *
            (self.Cameraa.getCameraToWorldPoint(event.getLocation()).x - self.ResolutionRatio.x / 2 - self.Player.x) +
            (self.Cameraa.getCameraToWorldPoint(event.getLocation()).y - self.ResolutionRatio.y / 2 - self.Player.y) *
            (self.Cameraa.getCameraToWorldPoint(event.getLocation()).y - self.ResolutionRatio.y / 2 - self.Player.y)
        )        
        var Speed = cc.v2((self.Cameraa.getCameraToWorldPoint(event.getLocation()).x - self.ResolutionRatio.x / 2 - self.Player.x) * self.Option.ShootSpeed / TouchALong,
            (self.Cameraa.getCameraToWorldPoint(event.getLocation()).y - self.ResolutionRatio.y / 2 - self.Player.y) * self.Option.ShootSpeed / TouchALong);
        self.Bacteria.ShootSpeed = Speed;
    },
    //  没有触摸输入时的镜头跟随数据
    CameraFollowP: function () {
        self = this;
        self.FPosX = this.Player.x;    // 视角应跟随的x坐标
        self.FPosY = this.Player.y;    // 视角应跟随的y坐标
        self.FRatx = 1;    // x轴上缩放倍数
        self.FRaty = 1;     // y轴上缩放倍数

    },
    // 有触摸输入时的镜头跟随数据
    CameraFollowT: function (self) {
        if (Math.abs(self.TouchxinC) > self.ResolutionRatio.x / 2 - 100){ // 触摸在边缘
            self.FPosX = self.node.x + self.Option.ShootSpeed * Math.sign(self.TouchxinC);
            if(Math.abs(self.FPosX) >  self.RingRadius.x / 2){
                self.FPosX = self.RingRadius.x / 2 * Math.abs(self.TouchxinC);
            }
            if(self.TouchxinC > 0){
                self.MoveArrow[2].active = true;
            }
            else{
                self.MoveArrow[3].active = true;
            }
        }
        else{
            if(self.MoveArrow[2].active){
                self.MoveArrow[2].getComponent("MoveArrow").Disappear();
            }
            if(self.MoveArrow[3].active){
                self.MoveArrow[3].getComponent("MoveArrow").Disappear();
            }
        }
        if (Math.abs(self.TouchyinC) > self.ResolutionRatio.y / 2 - 200){ 
            self.FPosY = self.node.y + self.Option.ShootSpeed * Math.sign(self.TouchyinC);
            if(Math.abs(self.FPosY) >  self.RingRadius.y / 2){
                self.FPosY = self.RingRadius.y / 2 * Math.sign(self.TouchyinC);
            }
            if(self.TouchyinC > 0){
                self.MoveArrow[0].active = true;
            }
            else{
                self.MoveArrow[1].active = true;
            }
        }
        else{
            if(self.MoveArrow[0].active){
                self.MoveArrow[0].getComponent("MoveArrow").Disappear();
            }
            if(self.MoveArrow[1].active){
                self.MoveArrow[1].getComponent("MoveArrow").Disappear();
            }
        }

        // 使玩家正好在镜头边缘
        var CPdisX = Math.abs(self.node.x - self.Player.x); //玩家到镜头中心的距离
        self.FRatx = 0.5 * self.ResolutionRatio.x / ( CPdisX + self.Player.width / 2);
        var CPdisY = Math.abs(self.node.y - self.Player.y);
        self.FRaty = 0.5 * self.ResolutionRatio.y / ( CPdisY + self.Player.width / 2);
    },
    // 根据跟随数据进行镜头跟随
    CameraFollowData:function () {
        var self = this;
        // 先缩放再移动
        var RatioValue = (self.FRatx < self.FRaty)? self.FRatx : self.FRaty;
        if(RatioValue < self.Rlimit){
            RatioValue = self.Rlimit;
        }
        else if (RatioValue > 1){
            RatioValue = 1;
        }
        self.Cameraa.zoomRatio = self.Lerp(self.Cameraa.zoomRatio, RatioValue, 0.005);

        var selfnodex = self.Lerp(self.node.x, self.FPosX, this.Option.ShootSpeed / 2);
        if(Math.abs(selfnodex) + 0.5 * self.ResolutionRatio.x / self.Cameraa.zoomRatio > 
        self.GameSize + self.BoundaryWidth){
            self.node.x = self.Lerp(self.node.x, Math.sign(self.node.x) * (self.GameSize + self.BoundaryWidth - 
                0.5 * self.ResolutionRatio.x / self.Cameraa.zoomRatio), this.Option.ShootSpeed) ;
        }
        else self.node.x = selfnodex;

        var selfnodey = self.Lerp(self.node.y, self.FPosY, this.Option.ShootSpeed / 2);
        if(Math.abs(selfnodey) + 0.5 * self.ResolutionRatio.y / self.Cameraa.zoomRatio > 
        self.GameSize + self.BoundaryWidth){
            self.node.y = self.Lerp(self.node.y, Math.sign(self.node.y) * (self.GameSize + self.BoundaryWidth - 
                0.5 * self.ResolutionRatio.y / self.Cameraa.zoomRatio), this.Option.ShootSpeed);
        }
        else  self.node.y = selfnodey;
    },

    onLoad() {
        this.Option = this.GM.Option;
        this.Cameraa = this.node.getComponent(cc.Camera);
        this.Bacteria = this.Player.getComponent("Bacteria");
        this.YRLimit = this.ResolutionRatio.y / (this.GameSize * 2 + 2 * this.BoundaryWidth);
        this.XRLimit = this.ResolutionRatio.x / (this.GameSize + this.RingRadius + this.BoundaryWidth);
        this.Rlimit = (this.YRLimit > this.XRLimit)? this.YRLimit : this.XRLimit;
    },

    start() {
        //console.log(this.Cameraa.zoomRatio);
        this.setInput();
    },

    update() {
        //console.log(this.FPosX + "," + this.FPosY + "---------" + this.FRatx + ", " + this.FRaty)
        // RATIO
        if (this.NOtouching) {
            this.CameraFollowP();
        }
        else this.CameraFollowT(this);
        this.CameraFollowData();
        this.UICamera.setContentSize(self.ResolutionRatio.x / self.Cameraa.zoomRatio, self.ResolutionRatio.y / self.Cameraa.zoomRatio);
    },

    Lerp: function (start, end, lerped) {
        if (typeof (start) == typeof (end) && typeof (start) == typeof (lerped)) {
            if (start < end) {
                start += lerped;

                if (start > end) {
                    start = end;
                }
            }
            else if (start > end) {
                start -= lerped;

                if (start < end) {
                    start = end;
                }
            }
        }
        return start;
    }

});